package org.candan.spider.task.xml;

import org.candan.spider.bean.DataSift;
import org.candan.spider.bean.Result;
import org.candan.spider.util.DateUtil;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;


public class DataSiftParse {

	/**
	 * @category DataFilter转换成XML
	 * @param plan
	 * @return
	 */
	public Element dataFilterToXML(DataSift result){
		Element el = new DefaultElement(XMLProperty.dataFilter);
		el.addAttribute(XMLProperty.regex, result.getRegex());
		el.addAttribute(XMLProperty.fromDate, DateUtil.getDateStr(result.getFromDate()));
		el.addAttribute(XMLProperty.toDate, DateUtil.getDateStr(result.getToDate()));
		el.addAttribute(XMLProperty.keywords, result.getKeywords());
		el.addAttribute(XMLProperty.range, result.getRange()+"");
		el.addAttribute(XMLProperty.maxDownload, result.getMaxDownload()+"");
		return el;
	}
	
	/**
	 * @category xml转换成DataFilter实体类
	 * @param el
	 * @return
	 */
	public DataSift  xmlToDataFilter(Element el){
		DataSift result = new DataSift();
		result.setRegex(el.attributeValue(XMLProperty.regex));
		result.setFromDate(DateUtil.getDate(el.attributeValue(XMLProperty.fromDate)));
		result.setToDate(DateUtil.getDate(el.attributeValue(XMLProperty.toDate)));
		result.setKeywords(el.attributeValue(XMLProperty.keywords));
		result.setRange(DateUtil.getInt(el.attributeValue(XMLProperty.range)));
		result.setMaxDownload(DateUtil.getInt(el.attributeValue(XMLProperty.maxDownload)));
		return result;
	}
}
